<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd(null)"
          v-hasPermi="['system:dept:add']"
        >新增</el-button>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="findAllData">搜索</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="findAllData"></right-toolbar> -->
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="treeList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'child', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="categoryName" label="分类名称" width="260"></el-table-column>
      <el-table-column prop="sortNum" label="排序" width="200"></el-table-column>
      <el-table-column label="修改时间" align="center" prop="modifyDate" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dept:edit']"
          >修改</el-button>
    <!--      <el-button
            size="mini"
            type="text"
            v-if="scope.row.catLevel == 1"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:dept:add']"
          >新增</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dept:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="上级分类" prop="parentId">
              <el-select v-model="form.parentId" placeholder="请选择上级分类">
                <el-option
                  v-for="dict in parentCate"
                  :key="dict.id"
                  :label="dict.categoryName"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类名称" prop="categoryName">
              <el-input v-model="form.categoryName" placeholder="请输入分类名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="sortNum">
              <el-input-number v-model="form.sortNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { findAll, saveCate, modifyCate, delCategory, findParentId } from "@/api/project/category";
import Treeselect from "@riophae/vue-treeselect";
import DataStts from '@/enums/DataStts'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Dept",
  // dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: false,
      treeList:[],
      parentCate:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 表单参数
      form: {
        id:null,
        categoryName:null,
        parentId:null,
        sortNum:null
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级分类不能为空", trigger: "blur" }
        ],
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        sortNum: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.findAllData();
  },
  methods: {
    findAllData(){
      findAll().then(res=>{
        this.treeList = res;
        this.loading = false;
      })
    },
    queryParentId(parentId){
      findParentId(parentId).then( res=>{
        this.parentCate = res;
         if(res == undefined || res.length <=0){
           this.parentCate =[];
         }
         this.parentCate.push({"id":"0","categoryName":"顶级分类"});
      });
    },
    handleAdd(row){
      this.reset();
      let parentId ="0";
      // if (row != undefined && row.id != undefined) {
      //    parentId = row.parentId;
      // }
      // this.queryParentId(parentId);
      // this.form.parentId = 0;
      this.open = true;
      this.title = "添加分类";
    },
    handleUpdate(row){
        this.form= {
          id:row.id,
          categoryName:row.categoryName,
          parentId:row.parentId,
          sortNum:row.sortNum
        },
        this.queryParentId(row.parentId)
        this.open = true;
        this.title = "修改分类";
    },
    handleDelete(row){
      let _this = this;
      this.$modal.confirm('确认要删除"' + row.categoryName + '"分类吗？').then(function() {
         delCategory(row.id).then(res=>{
           _this.$modal.alert("删除成功");
            _this.findAllData();
         });
      });

    },
    toggleExpandAll(){
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.parentId =0;
          if (this.form.id != undefined) {
            modifyCate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.findAllData();
            });
          } else {
            saveCate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.findAllData();
            });
          }
        }
      });
    },
    cancel(){
      this.open = false;
      this.reset();
    },
    reset(){
      this.form= {
        id:null,
        categoryName:null,
        parentId:null,
        sortNum:null
      }
    },
  }
};
</script>
